#ifndef _EVENT_H_
#define _EVENT_H_

struct event{
    struct event *prev;
    struct event *next;
    int nodeValue ;
};

struct eventList{
    struct event *head;
    struct event *tail;
    int nodeNum;
};


#define MeventListInit(_L)  \
    _L->head    = NULL;     \
    _L->tail    = _L->head; \
    _L->nodeNum = 0;        

#define MeventAdd(_L,N)                    \
    if (_L->head == NULL){                 \
        _L->head = N;                      \
        N->prev = NULL ;                   \
        N->next = NULL ;                   \
    }else{                                 \
        _L->tail->next = N;                \
        N->prev = _L->tail;                \
        N->next = NULL ;                   \
    }                                      \
    _L->tail = N;                          \
    _L->nodeNum += 1;                       

#define MeventRm(_L,N)                            \
    if(N == _L->head){                            \
        if(N->next == NULL){                      \
                _L->head    = NULL;               \
                _L->tail    = _L->head;           \
                _L->nodeNum = 0;                  \
        }else{                                    \
            _L->head = N->next;                   \
        }                                         \
    }else if(N == _L->tail){                      \
        if(N->prev == NULL){                      \
            _L->head    = NULL;                   \
            _L->tail    = _L->head;               \
            _L->nodeNum = 0;                      \
        }else{                                    \
            _L->tail = N->prev;                   \
            _L->tail->next = NULL ;               \
        }                                         \
    }                                             \
    N->prev->next = N->prev->next;                \
    N->next->prev = N->prev;                      \
    _L->nodeNum -= 1;


static inline int eventListInit(struct eventList * _L){
    if (_L == (struct eventList*)NULL){
        return -1;
    }
    _L->head    = NULL;
    _L->tail    = _L->head;
    _L->nodeNum = 0;
    return 0;
}

static inline int eventAdd(struct eventList * _L, struct event *_ev){
    if (_L == (struct eventList*)NULL || _ev == (struct event *)NULL ){
        return -1;
    }

    if (_L->head == (struct event *)NULL){
        _L->head = _ev;
        _ev->prev = NULL ;
        _ev->next = NULL ;
    }else{
        _L->tail->next = _ev;
        _ev->prev = _L->tail;
        _ev->next = NULL ;
    }
    _L->tail = _ev;
    _L->nodeNum += 1;
    return 0;
}

static inline int eventRm(struct eventList * _L, struct event *_ev){
    if (_L == (struct eventList*)NULL || _ev == (struct event *)NULL ){
        return -1;
    }
    if(_ev == _L->head){
        if(_ev->next == (struct event *)NULL){
            return eventListInit(_L);
        }else{
            _L->head = _ev->next;
        }
    }else if(_ev == _L->tail){
        if(_ev->prev == (struct event *)NULL){
            return eventListInit(_L);
        }else{
            _L->tail = _ev->prev;
            _L->tail->next = NULL ;
        }
    }
    _ev->prev->next = _ev->next;
    _ev->next->prev = _ev->prev;
    _L->nodeNum -= 1;
    return 0;
}
#endif